package com.liu.webstore.service.impl;

import com.liu.webstore.entity.DiscussPost;
import com.liu.webstore.repository.DiscussPostMapper;
import com.liu.webstore.service.DiscussPostService;
import com.liu.webstore.util.SensitiveWordFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;
import java.util.Objects;

/**
 * @Author LiuYumei
 * @Date 2022/7/11 23:19
 */
@Service
public class DiscussPostServiceImpl implements DiscussPostService {
    private static Logger log = LoggerFactory.getLogger(DiscussPostServiceImpl.class);

    @Autowired
    private SensitiveWordFilter sensitiveWordFilter;

    @Autowired
    private DiscussPostMapper discussPostMapper;

    @Override
    public List<DiscussPost> selectDiscussPosts(int userId, int offert, int limit) {
        return discussPostMapper.selectDiscussPosts(userId, offert, limit);
    }

    @Override
    public int selectDiscussPostRows(int userId) {
        return discussPostMapper.selectDiscussPostRows(userId);
    }

    @Override
    public int addDiscussPost(DiscussPost discussPost) {
        if (Objects.isNull(discussPost)) {
            log.error("参数不能为空！");
        }
        // 转义HTML标记
        discussPost.setTitle(HtmlUtils.htmlEscape(discussPost.getTitle()));
        discussPost.setContent(HtmlUtils.htmlEscape(discussPost.getContent()));
        // 过滤敏感词
        discussPost.setTitle(sensitiveWordFilter.filter(discussPost.getTitle()));
        discussPost.setContent(sensitiveWordFilter.filter(discussPost.getContent()));
        return discussPostMapper.insertDiscussPost(discussPost);
    }

    @Override
    public DiscussPost findDiscussPost(int id) {
        return discussPostMapper.selectDiscussPostDetail(id);
    }

    @Override
    public int updateCommentCount(int id, int commentCount) {
        return discussPostMapper.updateCommentCount(id, commentCount);
    }

    @Override
    public int updateType(int id, int type) {
        return discussPostMapper.updateType(id, type);
    }

    @Override
    public int updateStatus(int id, int status) {
        return discussPostMapper.updateStatus(id, status);
    }
}
